package com.example.e_comm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.e_comm.dto.UserDTO;
import com.example.e_comm.dto.UserPageQuery;
import com.example.e_comm.dto.UserVO;
import com.example.e_comm.entity.User;
import com.example.e_comm.mapper.UserMapper;
import com.example.e_comm.service.UserService;
import com.example.e_comm.util.RedisUtil;
import com.example.e_comm.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class UserServiceImpl
        extends ServiceImpl<UserMapper, User>
        implements UserService {
    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private TokenUtil tokenUtil;

    @Override
    public void deleteUser(Long id) {

    }

    @Override
    public boolean register(User user) {
        // 查重：username、email
        if (this.count(new QueryWrapper<User>()
                .eq("username", user.getUsername())) > 0){
            throw new IllegalArgumentException("用户名已存在，请更换其它用户名！");
        }
        if (this.count(new QueryWrapper<User>()
                .eq("email", user.getEmail())) > 0) {
            throw new IllegalArgumentException("邮箱已被注册！");
        }
        // 密码加密
        if (user.getPassword() == null) {
            throw new IllegalArgumentException("密码不能为空！");
        }
        user.setPassword(DigestUtils.md5DigestAsHex(((String) user.getPassword()).getBytes()));

        user.setStatus(1);
        return this.save(user);
    }

    @Override
    public String login(String username, String password) {
        // 登录验证，验证是否正确输入username、password
        if (StringUtils.isBlank(username) && StringUtils.isBlank(password)) {
            throw new IllegalArgumentException("用户名或密码不能为空！");
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();//用于实现查询条件对象
        //根据用户名、密码查询状态为在的用户
        queryWrapper.eq("username", username)
                .eq("password", DigestUtils.md5DigestAsHex(password.getBytes()))
                .eq("status", 1);
        User user = this.getOne(queryWrapper);
        if (user != null) {
            throw new IllegalArgumentException("用户名或密码错误,请查证后重新操作！");
        }
        //为找到的用户生成token
        String token = tokenUtil.generateToken();
        //将生成的token保存到redis缓存中，同时设定有效时长
        redisUtil.set("user:token:"+token, user.getId().toString(), 30, TimeUnit.MINUTES);
        //建立用户id和token的映射关系
        redisUtil.set("user:id:"+user.getId(), token, 30, TimeUnit.MINUTES);
        return token;
    }

    @Override
    public User getUserInfo(String token) {
        //查找Redis缓存中保存的token
        if (!tokenUtil.validateToken(token)) {
            return null;
        }
        //从Redis缓存中获取用户id
        Object userIdObj = redisUtil.get("user:token:" + token);
        if (userIdObj == null) {
            return null;
        }
        try {
            Long userId = Long.valueOf(userIdObj.toString());//获取Redis缓存中的用户id
            //根据用户ID查找数据库
            User user = this.getById(userId);
            if (user != null){
                redisUtil.set("user:token:" + token, userId.toString(), 30, TimeUnit.MINUTES);
                redisUtil.set("user:id:" + userId, token, 30, TimeUnit.MINUTES);
            }
            return user;
        } catch (Exception e) {
            System.err.println("没有找到与这个token适配的User：" + e.getMessage());
            return null;
        }
    }

    @Override
    public boolean logout(String token) {
        User user = getUserInfo(token);
        if (user != null) {
            redisUtil.delete("user:token:" + token);
            redisUtil.delete("user:id:" + user.getId());
            return true;
        }
        return false;
    }
    public boolean vaildateToken(String token) {
        return redisUtil.hasKey("user:token:" + token);
    }

    @Override
    public User getUserByToken(String token) {
        return null;
    }

    @Override
    public IPage<UserVO> pageUser(UserPageQuery userPageQuery) {
        return null;
    }

    @Override
    public UserVO getUser(Long id) {
        return null;
    }

    @Override
    public Long createUser(UserDTO dto) {
        return null;
    }
}